package ide.ccf.ccdesigner.editors;

import org.eclipse.jface.action.Action;
import ide.ccf.ccdesigner.views.embedded.FormWindowW;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Event;

/**
 * 窗体编辑器操作类
 *
 */
public class DesignerAction extends Action
{
    private DesignerActionBarContributor contributor;
    private int actId;
    private boolean tool;
    private boolean hasicon;
    private int lastevent = 0;
    FormWindowW formwindow = null;

    /**
     * 构造器
     * @param contributor
     * @param actId
     * @param tool
     */
    public DesignerAction(DesignerActionBarContributor contributor, int actId, boolean tool)
    {
        this.actId = actId;
        this.contributor = contributor;
        this.tool = tool;
        hasicon = false;
        formwindow = contributor.activeEditor().formWindow();
        String strIcon;

        if (tool)
        {
            setText(formwindow.getNameByActionID(actId, false));

            if ((strIcon = toolIconFile(actId)) != null)
            {
                hasicon = true;
                try 
                {
                    setImageDescriptor(ImageDescriptor.createFromFile(getClass(), strIcon));
                } 
                catch (Exception e) 
                {
                    System.out.println(e.toString());
                }
            }

            this.setToolTipText(formwindow.getNameByActionID(actId, true));
            setEnabled(true);
        }
        else
        {
            setText(formwindow.getNameByActionID(actId, false));
            
            if (actId > 6) 
            {
                if ((strIcon = actionIconFile(actId)) != null)
                {
                    hasicon = true;

                    try 
                    {
                        setImageDescriptor(ImageDescriptor.createFromFile(getClass(), strIcon));
                    } 
                    catch (Exception e) 
                    {
                        System.out.println(e.toString());
                    }
                }
            }
            
            this.setToolTipText(formwindow.getNameByActionID(actId, true));
            setEnabled(formwindow.isEnabled(actId));
        }
    }

    /**
     * 使用事件触发机制运行
     */
    public void runWithEvent(Event event) 
    {
        if (event.time != lastevent) 
        {
            lastevent = event.time;
            run();
        }
    }

    /**
     * 运行
     */
    public void run()
    {
        FormWindowW formwindow = contributor.activeEditor().formWindow();

        if (tool)
        {
            formwindow.setCurrentTool(actId);
            contributor.updateTools();
        }
        else
        {
            formwindow.actionTrigger(actId);
        }
    }

    /**
     * 是否有图标
     * @return
     */
    public boolean hasIcon()
    {
        return hasicon;
    }

    /**
     * 动作图标文件
     * @param id
     * @return
     */
    private String actionIconFile(int id)
    {
        switch(id)
        {
	        case 7:
	            return "actionicons/editlower.png";
	            
	        case 8:
	            return "actionicons/editraise.png";
	            
	        case 9:
	            return "actionicons/edithlayout.png";
	            
	        case 10:
	            return "actionicons/editvlayout.png";
	            
	        case 11:
	            return "actionicons/edithlayoutsplit.png";
	            
	        case 12:
	            return "actionicons/editvlayoutsplit.png";
	            
	        case 13:
	            return "actionicons/editgrid.png";
	            
	        case 14:
	            return "actionicons/editbreaklayout.png";
	            
	        case 15:
	            return "actionicons/adjustsize.png";
        }
        return null;
    }

    /**
     * 工具图标文件
     * @param id
     * @return
     */
    private String toolIconFile(int id)
    {
        switch(id)
        {
	        case 0:
	            return "actionicons/widgettool.png";
	            
	        case 1:
	            return "actionicons/signalslottool.png";
	            
	        case 2:
	            return "actionicons/buddytool.png";
	            
	        case 3:
	            return "actionicons/tabordertool.png";
        }
        return null;
    }
    
    
}
